package com.lfz.manage.controller;

import com.lfz.common.exception.MsgException;
import com.lfz.common.vo.SysResult;
import com.lfz.manage.pojo.Job;
import com.lfz.manage.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job")
public class JobController {

    /*
    获取所有job记录
    url: /job
    method: get
    param: 无
    返回值示例:
    {
        "status": 200,
        "msg": "OK",
        "data": [
            {
                "createBy": 1533414680000,
                "createAt": "",
                "updateBy": 1533414686000,
                "updateAt": null,
                "jobId": "105227000",
                "eduLevel": "大专",
                "compId": "co4069180",
                "jobAddress": "湖北省武汉市光谷大道金融港A1栋",
                "city": "武汉",
                "jobUrl": "https://jobs.51job.com/wuhan/105227000.html?s=01&t=0",
                "releaseTime": 1533225600000,
                "workingExp": "无工作经验",
                "count": "10",
                "welfare": "带薪年假/专业培训/节日福利/全勤奖/绩效奖金/长期服务津贴/高温低温补贴/商业保险/五险一金/企业年金",
                "minSalary": 3000,
                "maxSalary": 8000,
                "jobName": "客服或销售2选1",
                "jobDesc": "泰康人寿招募英雄豪杰，需要2个你岗位一：...",
                "status": 0
            }
        ],
        "ok": true
    }


    获取一条job记录
    url: /job/{jobId}
    method: get
    param:
        jobId  字符串  必需
    返回值示例：
    {
        "status": 200,
        "msg": "OK",
        "data": {
            "createBy": 1533414680000,
            "createAt": "",
            "updateBy": 1533414686000,
            "updateAt": null,
            "jobId": "105227000",
            "eduLevel": "大专",
            "compId": "co4069180",
            "jobAddress": "湖北省武汉市光谷大道金融港A1栋",
            "city": "武汉",
            "jobUrl": "https://jobs.51job.com/wuhan/105227000.html?s=01&t=0",
            "releaseTime": 1533225600000,
            "workingExp": "无工作经验",
            "count": "10",
            "welfare": "带薪年假/专业培训/节日福利/全勤奖/绩效奖金/长期服务津贴/高温低温补贴/商业保险/五险一金/企业年金",
            "minSalary": 3000,
            "maxSalary": 8000,
            "jobName": "客服或销售2选1",
            "jobDesc": "泰康人寿招募英雄豪杰，需要2个你岗...",
            "status": 0
        },
        "ok": true
    }


    添加一条记录
    url: /job
    method: put
    param: Job对象
    参数示例：字段全部必需
        jobId:105266793
        eduLevel:大专
        compId:co3726993
        jobAddress:人民北路苹果城1期
        city:大理
        jobUrl:https://jobs.51job.com/dali/105266793.html?s=01&t=0
        releaseTime:2018-08-03 00:00:00
        workingExp:2年经验
        count:2
        welfare:弹性工作/带薪年假/五险一金/绩效奖金/全勤奖/节日福利/专业培训
        minSalary:4500
        maxSalary:6000
        jobName:悟空找房大理分公司招聘招商BD（商务拓展）
        jobDesc:岗位职责：1、负责公司悟空找房房产平台...
        createBy: 1533414680000,
        createAt: uuid, 创建者的id
        updateBy: 1533414686000,
        updateAt: uuid, 更新者的id

    返回值示例：
    {
        "status": 200,
        "msg": "OK",
        "data": null,
        "ok": true
    }


    更新一条记录
    url: /job
    method: post
    param: Job对象
    参数示例：
        jobId:105266793  必需字段
        eduLevel:大专
        compId:co3726993
        jobAddress:人民北路苹果城1期
        city:大理
        jobUrl:https://jobs.51job.com/dali/105266793.html?s=01&t=0
        releaseTime:2018-08-03 00:00:00
        workingExp:2年经验
        count:2
        welfare:弹性工作/带薪年假/五险一金/绩效奖金/全勤奖/节日福利/专业培训
        minSalary:4500
        maxSalary:6000
        jobName:悟空找房大理分公司招聘招商BD（商务拓展）
        jobDesc:岗位职责：1、负责公司悟空找房房产平台...
        updateBy: 1533414686000,   必需字段
        updateAt: uuid, 必需字段

    返回值示例：
    {
        "status": 200,
        "msg": "OK",
        "data": null,
        "ok": true
    }


    删除一条记录
    url: /job/{jobId}
    method: delete
    param: jobId 字符串类型
    返回值示例：
    {
        "status": 200,
        "msg": "OK",
        "data": null,
        "ok": true
    }



     */


    @Autowired
    private JobService service;

    @GetMapping
    public SysResult listAll(){
        try {
            List<Job> list = service.listAll();
            return SysResult.oK(list);
        }catch (Exception e) {
            e.printStackTrace();
            return SysResult.OPERATE_FAILED;
        }
    }

    @GetMapping("/{jobId}")
    public SysResult getJobById(@PathVariable String jobId){
        try {
            Job job = service.getJobById(jobId);
            return SysResult.oK(job);
        }catch (Exception e) {
            e.printStackTrace();
            return SysResult.OPERATE_FAILED;
        }
    }

    @PutMapping
    public SysResult addJob(Job job){
        try {
            service.addJob(job);
            return SysResult.oK();
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            SysResult result = SysResult.OPERATE_FAILED;
            result.setMsg("记录已经存在！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.OPERATE_FAILED;
        }
    }

    @PostMapping
    public SysResult updateJob(Job job){
        try {
            service.updateJob(job);
            return SysResult.oK();
        } catch (MsgException e) {
            e.printStackTrace();
            SysResult result = SysResult.OPERATE_FAILED;
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.OPERATE_FAILED;
        }
    }

    @DeleteMapping("/{jobId}")
    public SysResult deleteJob(@PathVariable String jobId) {
        try {
            service.deleteJob(jobId);
            return SysResult.oK();
        } catch (MsgException e) {
            e.printStackTrace();
            SysResult result = SysResult.OPERATE_FAILED;
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.OPERATE_FAILED;
        }
    }


}
